{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "sum= 25\n"
     ]
    }
   ],
   "source": [
    "i = 1\n",
    "sum = 0\n",
    "while i <= 10:\n",
    "    sum = sum + i\n",
    "    i = i+2\n",
    "print(\"sum=\", sum)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Input a String:444rer\n",
      "444RER"
     ]
    }
   ],
   "source": [
    "st = input('Input a String:')   #  st.upper()\n",
    "i = 0\n",
    "while  i < len(st):\n",
    "    if  'a' <= st[i] <= 'z':\n",
    "        print(st[i].upper(), end = '')\t\n",
    "    else:\n",
    "        print(st[i], end = '') \n",
    "    i = i + 1   # 注意此句不要缩进到else内部(死循环)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Input a String:444rer\n",
      "444RER"
     ]
    }
   ],
   "source": [
    "st = input('Input a String:')  # st.lower()\n",
    "for  s  in  st:\n",
    "    if  'a' <= s <= 'z':\n",
    "        print(s.upper(), end = '')\n",
    "    else:\n",
    "        print(s, end = '')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "输入年龄:44\n",
      "输入年龄:343\n",
      "输入年龄:33\n",
      "输入年龄:25\n",
      "age= 25\n"
     ]
    }
   ],
   "source": [
    "try:\n",
    "    while  True: # 一种常见的表达\n",
    "        age = int(input('输入年龄:'))\n",
    "        if  16 <= age <= 25:\n",
    "            break\n",
    "\n",
    "    print('age=', age)\n",
    "except  Exception:\n",
    "    print(\"输入错误\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2 - 86\n",
      "4 - 24\n",
      "6 - 28\n",
      "7 - 2\n",
      "9 - 12\n",
      "13 - 2\n",
      "14 - 16\n"
     ]
    }
   ],
   "source": [
    "from random import randint\n",
    "a = tuple((randint(1,100) for i in range(15))) # 生成元组\n",
    "for i, v in enumerate(a):\n",
    "    if v % 2 == 0:\n",
    "        print(i, '-', v)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "第一个优秀学生是7，他的成绩为97\n"
     ]
    }
   ],
   "source": [
    "from random import randint\n",
    "score = [randint(1,100) for x in range(50)]    # 随机成绩\n",
    "for i, s in enumerate(score):\n",
    "    if s >= 90:\n",
    "        print(\"第一个优秀学生是{}，他的成绩为{}\".format(i, s))\n",
    "        break   # 找到第一个后，即跳出循环\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[72, 39, 82, 89, 29, 17, 30, 97, 48, 44, 8, 87, 86, 93, 2, 19, 39, 67, 72, 65, 33, 23, 65, 51, 59, 69, 61, 91, 50, 34, 14, 73, 48, 20, 22, 71, 94, 11, 19, 7, 14, 38, 60, 69, 37, 13, 82, 58, 70, 24]\n"
     ]
    }
   ],
   "source": [
    "print (score)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " 1*1 = 1\n",
      " 1*2 = 2 2*2 = 4\n",
      " 1*3 = 3 2*3 = 6 3*3 = 9\n",
      " 1*4 = 4 2*4 = 8 3*4 =12 4*4 =16\n",
      " 1*5 = 5 2*5 =10 3*5 =15 4*5 =20 5*5 =25\n",
      " 1*6 = 6 2*6 =12 3*6 =18 4*6 =24 5*6 =30 6*6 =36\n",
      " 1*7 = 7 2*7 =14 3*7 =21 4*7 =28 5*7 =35 6*7 =42 7*7 =49\n",
      " 1*8 = 8 2*8 =16 3*8 =24 4*8 =32 5*8 =40 6*8 =48 7*8 =56 8*8 =64\n",
      " 1*9 = 9 2*9 =18 3*9 =27 4*9 =36 5*9 =45 6*9 =54 7*9 =63 8*9 =72 9*9 =81\n"
     ]
    }
   ],
   "source": [
    "for i in range(1,10):        # 外\n",
    "    for j in range(1,i+1):  # 内，内层的j受限于i，而i在外层是1-10\n",
    "        print('{:2d}*{:<2d}={:2d}'.format(j, i, j*i), end = '')  # 不换行,2表示占2位\n",
    "    print()      "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "错误: division by zero\n",
      "错误: division by zero\n",
      "错误: division by zero\n",
      "错误: division by zero\n",
      "10.0\n"
     ]
    }
   ],
   "source": [
    "from random import randint\n",
    "sm = 0\n",
    "i = 0\n",
    "while i < 10:\n",
    "    try:\n",
    "        sm = sm + 1 / randint(0,1)\n",
    "        i = i + 1\n",
    "    except Exception as e:\n",
    "        print(\"错误:\",  e)\n",
    "else:\n",
    "    print(sm)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
